package ebj.common.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import ebj.common.domain.Person;
import ebj.common.utils.Utils;


/**
 * 反射实例3：通过Class调用其他类中的构造函数 （也可以通过这种方式通过Class创建其他类的对象）
 * @author zhouxiangyong
 * @version $Id$
 */
public class ReflectClient3 {
	public static void main(String[] args) {
		String clazzName = Utils.getProperty("Person");
		Class<?> clazz = null;
		Person person1 = null;
		Person person2 = null;
		Person person3 = null;
		Person person4 = null;
		
		try {
			clazz = Class.forName(clazzName);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		Constructor<?>[] constructors = clazz.getConstructors();
		for (int i = 0; i < constructors.length; i++) {
			System.out.println("构造方法: " + String.valueOf(i));
			Class<?>[] parmsTypes = constructors[i].getParameterTypes();
			System.out.println("参数个数: " + parmsTypes.length);
			for (Class<?> class1 : parmsTypes) {
				System.out.println(class1.getSimpleName());
			}
			System.out.println("=============**==============");
		}
		
		try {
			person2 = (Person) constructors[0].newInstance("王五");
			person4 = (Person) constructors[1].newInstance("赵六", 27);
			person3 = (Person) constructors[2].newInstance(23);
			person1 = (Person) constructors[3].newInstance();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		System.out.println(person1);
		System.out.println(person2);
		System.out.println(person3);
		System.out.println(person4);
	}
}
